package work.parabola.keyboardswitch.controller;

import org.apache.catalina.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import work.parabola.keyboardswitch.common.ResponseCode;
import work.parabola.keyboardswitch.common.RoleEnum;
import work.parabola.keyboardswitch.common.ServerResponse;
import work.parabola.keyboardswitch.common.UserConst;
import work.parabola.keyboardswitch.pojo.UserSession;
import work.parabola.keyboardswitch.pojo.query.keyswitch.AddSwitchQuery;
import work.parabola.keyboardswitch.pojo.query.keyswitch.EditSwitchQuery;
import work.parabola.keyboardswitch.pojo.query.keyswitch.ListQuery;
import work.parabola.keyboardswitch.service.ISwitchService;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/switch/")
public class SwitchController {

    @Autowired
    public ISwitchService iSwitchService;

    @PostMapping("getSwitchList.do")
    public ServerResponse getSwitchList(HttpSession session, @RequestBody ListQuery listQuery) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }
        return this.iSwitchService.getList(listQuery);
    }

    @GetMapping("getSwitchDetail.do/{id}")
    public ServerResponse getSwitchDetail(HttpSession session, @PathVariable Integer id) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }
        return this.iSwitchService.getSwitchDetail(id);
    }

    @PostMapping("editSwitch.do")
    public ServerResponse editSwitch(HttpSession session, @RequestBody EditSwitchQuery editSwitchQuery){
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }
        return this.iSwitchService.editSwitch(editSwitchQuery);
    }

    @PostMapping("addSwitch.do")
    public ServerResponse addSwitch(HttpSession session, @RequestBody AddSwitchQuery addSwitchQuery){
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }
        return this.iSwitchService.addSwitch(addSwitchQuery);
    }
}
